package com.hdf.etdemo.friend;
import java.io.IOException;
import java.util.ArrayList;

import com.hdf.easytools.base.EasyToolsActivity;
import com.hdf.etdemo.R;
import com.hdf.etdemo.R.id;
import com.hdf.etdemo.R.layout;
import com.hdf.etdemo.friend.adapter.UpLoadGridAdapter;
import com.hdf.etdemo.friend.utils.BitmapCompress;
import com.hdf.etdemo.image.ImageSelectActivity;
import com.hdf.etdemo.image.select.MultiImageSelectorActivity;
import com.hdf.etdemo.image.select.PreViewActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class WritMoodActivity extends EasyToolsActivity {
	private GridView imageData;
	private UpLoadGridAdapter gridadapter;
	private static final int REQUEST_IMAGE = 2;
	private static final int MAX_IMG=9;//最大图片数量
	private ArrayList<String> mSelectPath;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_writ_mood);
		/** 设置标题显示 */
		this.TitleVisib(0, 1, 1, 1, 0);
		/** 绑定标题点击事件 */
		this.TitleClick(0, 1, 0, 1, 0);
		/** 设置标题样式ʽ */
		this.TitleStyle(null, R.drawable.et_back, "写心情", "发送", 0);
		TiTRight.setTextColor(getResources().getColor(R.color.white));
		initView();
	}
	private void initView()
	{
		imageData=(GridView) findViewById(R.id.imagedata);
		gridadapter=new UpLoadGridAdapter(WritMoodActivity.this);
		imageData.setAdapter(gridadapter);
		imageData.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				if (arg2 == BitmapCompress.bmp.size()) {
					if(arg2==MAX_IMG)
					{
						GoPreView();
						return;
					}
					Intent intent = new Intent(WritMoodActivity.this, MultiImageSelectorActivity.class);
					// 是否显示拍摄图片
					intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
					// 最大可选择图片数量
					intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT,MAX_IMG);
					// 选择模式
					intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
					// 默认选择
					if (mSelectPath != null && mSelectPath.size() > 0) {
						intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
					}
					startActivityForResult(intent, REQUEST_IMAGE);
				} else {
					GoPreView();
				}
			}
		});
	}
	public void GoPreView()
	{
		Intent intent=new Intent(WritMoodActivity.this, PreViewActivity.class);
    	Bundle b=new Bundle();
    	b.putStringArrayList("imglist", mSelectPath);
    	intent.putExtra("b", b);
    	startActivity(intent);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_IMAGE) {
			if (resultCode == RESULT_OK) {
				mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
				BitmapCompress.bmp.clear();
				for (int i = 0; i < mSelectPath.size(); i++) {
					try {
						BitmapCompress.bmp.add(BitmapCompress.revitionImageSize(mSelectPath.get(i)));
						gridadapter.notifyDataSetChanged();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			Log.d("info",BitmapCompress.bmp.size()+"" );
		}
	}
	 @Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.left_img:
				bitmapClear();
				break;
			case R.id.right_text:
				
				break;
			default:
				break;
			}
		}
	public void bitmapClear()
	{
		
		BitmapCompress.bmp.clear();
		BitmapCompress.max = 0;
		//super.onDestroy();
		
		finish();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按两下退出程序
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			bitmapClear();
			return true;
		} 
		return super.onKeyDown(keyCode, event);
	}
}
